/**
 *******************************************************************************
 * @brief       Provide a brief description of the module here (Single line).
 * 
 * @author      Ashwin
 * 
 * @file        PalDs1629.h
 * 
 * @version
 * 
 * @date        Feb 21, 2012 7:12:42 PM
 * 
 * @details     Provide a detailed description of the module here (Multi-line).
 * 
 * @pre         Mention any preconditions required to use this module.
 * 
 * @bug         List any bugs in this module.
 * 
 * @note        This module has been build on top of the original DS1307 driver.
 * 
 * @warning     List any warnings against using the objects of this module.
 *******************************************************************************
 * @copyright 2012
 * <a href="http://www.afflatustech.com">Afflatus Techtronics</a>
 * and <a href="http://www.ashwinvijayakumar.co.nr">Ashwin Vijayakumar</a>
 * @n@n
 * This work is licensed under the Creative Commons
 * Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy
 * of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or
 * send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain
 * View, California, 94041, USA.
 *******************************************************************************
 */

/**@cond Include guard symbol - Protect against multiple inclusions  @endcond */

#ifndef PAL_DS1629_H_
#define PAL_DS1629_H_

/**@cond ***********************************************************************
 * Add #include "PalDs1629.h" in source files that need access to the below
 * listed declarations.
 ******************************************************************* @endcond */

/**@cond -----------------------------------------------------------------------
 * Includes section
 * Add all #include here.
 * ----------------------------------------------------------------- @endcond */

/**@cond System headers -------------------------------------------- @endcond */

/**@cond Local headers --------------------------------------------- @endcond */
#include "Std_Types.h"
#include "Platform_Types.h"

/**@cond -----------------------------------------------------------------------
 * typedef section (Project scope)
 * Add typedef's with project scope here.
 * Capitalization: Upper_Camel_Case.
 * ----------------------------------------------------------------- @endcond */

/* Indexing based on tm_wday (GCC's time.h) */
typedef enum
{
  SUNDAY        = 0x00,
  MONDAY        = 0x01,
  TUESDAY       = 0x02,
  WEDNESDAY     = 0x03,
  THURSDAY      = 0x04,
  FRIDAY        = 0x05,
  SATURDAY      = 0x06,
  NO_OF_WDAY    = 0x07
} PalDs1629_DayOfWeekType;

typedef enum
{
  AM    = 0x00,
  PM    = 0x01
} PalDs1629_AmPmType;

typedef enum
{
  _24_HOUR    = 0x00,
  _12_HOUR    = 0x01
} PalDs1629_HourModeType;

typedef struct tm       PalDs1629_BreakdownTimeType;

typedef char PalDs1629_BreakdownTimeASCIIType;

/* FIXME: Move this typedef to I2C library */
/* I2C Device Address */
/** @brief I2C Device Address
 * @details
 */
typedef unsigned char           I2c_AddressType;

/* DS1629 Address Type */
/** @brief DS1629 Address Type
 * @details This type shall be used to declare variables to hold the DS1629
 * memory location address
 */
typedef unsigned char           PalDs1629_AddressType;

/**
 * @brief DS1629 SECONDS register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's SECONDS register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8 seconds       :7;     /**< Entry Mode Set, S: Shift display. */
    uint8 ch   :1;     /**< Entry Mode Set, I/D: Increment/Decrement cursor position. */
  } bits;
} PalDs1629_SecondsRegType;

/**
 * @brief DS1629 Minutes register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's Minutes register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       minutes                 : 7;
    uint8                       reserved_minutes_7      : 1;
  } bits;
} PalDs1629_MinutesRegType;

/**
 * @brief DS1629 HOUR Register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's HOUR register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8 hours       :5;     /**< Entry Mode Set, S: Shift display. */
    PalDs1629_AmPmType ampm   :1;     /**< Entry Mode Set, I/D: Increment/Decrement cursor position. */
    uint8 mode       :1;     /**< Display On/Off Control, B: Blink cursor. */
    uint8 reserved_8    : 1;
  } bits;
} PalDs1629_HourRegType;

/**
 * @brief DS1629 Day register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's Minutes register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       day                     : 3;
    uint8                       reserved_3_7        : 5;
  } bits;
} PalDs1629_DayRegType;

/**
 * @brief DS1629 Date register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's Date register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       date                    : 6;
    uint8                       reserved_6_7       : 2;
  } bits;
} PalDs1629_DateRegType;

/**
 * @brief DS1629 Month register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's Month register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       month                   : 5;
    uint8                       reserved_5_7      : 3;
  } bits;
} PalDs1629_MonthRegType;

/**
 * @brief DS1629 Year register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's Year register.
 */
typedef uint8                 PalDs1629_YearRegType;

/**
 * @brief DS1629 Timekeeper Register Type.
 * @details This type shall be used to declare a variable which will hold the
 * Timekeeper Register contents for DS1629.
 */
typedef struct
{
  PalDs1629_SecondsRegType         SecondsReg;        /**< Function Set, F: Font. */
  PalDs1629_MinutesRegType         MinutesReg;        /**< Function Set, F: Font. */
  PalDs1629_HourRegType            HoursReg;          /**< Display On/Off Control, C: Show cursor. */
  PalDs1629_DayRegType             DayReg;            /**< Display On/Off Control, C: Show cursor. */
  PalDs1629_DateRegType            DateReg;           /**< Function Set, F: Font. */
  PalDs1629_MonthRegType           MonthReg;          /**< Display On/Off Control, C: Show cursor. */
  PalDs1629_YearRegType            YearReg;           /**< Function Set, F: Font. */
  uint8                         Reserved;
} PalDs1629_TimekeeperRegType;

/**
 * @brief DS1629 Configuration Type.
 * @details This type shall be used to declare a variable which will hold the
 * configuration parameters for DS1629.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8 _1SHOT        : 1;        /**< Provide struct data type1's brief description here. */
    uint8 POL           : 1;        /**< Provide struct data type3's brief description here. */
    uint8 reserved      : 2;        /**< Provide struct data type2's brief description here. */
    uint8 NVB           : 1;        /**< Provide struct data type1's brief description here. */
    uint8 TLF           : 1;        /**< Provide struct data type3's brief description here. */
    uint8 THF           : 1;        /**< Provide struct data type2's brief description here. */
    uint8 DONE          : 1;        /**< Provide struct data type1's brief description here. */
  } bits;
} PalDs1629_ControlRegType;

/**
 * @brief DS1629 Configuration Type.
 * @details This type shall be used to declare a variable which will hold the
 * configuration parameters for DS1629.
 */
typedef struct
{
  PalDs1629_HourRegType            HoursReg;          /**< Display On/Off Control, C: Show cursor. */
  PalDs1629_ControlRegType         ControlReg;        /**< Display On/Off Control, C: Show cursor. */
} PalDs1629_ConfigType;

/* DS1629 Address Type */
/** @brief DS1629 Address Type
 * @details This type shall be used to declare variables to hold the DS1629
 * register address
 */
typedef unsigned char           DS1629_AddressType;

/* DS1629 Temperature Register Type. */
/**
 * @brief DS1629 Temperature Register Type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1629's Temperature register.
 * @warning The DS1629 chip sends Temp MSB first and then LSB, hence the
 * reversed byte packing.
 */
typedef union
{
  uint16 word;

  struct
  {
    uint8  byte_H;
    uint8  byte_L;
  } bytes;

  struct
  {
    uint16 temp_H       : 7;        /**< Provide struct data type2's brief description here. */
    uint16 sign         : 1;        /**< Provide struct data type1's brief description here. */
    uint16 unused       : 7;        /**< Provide struct data type3's brief description here. */
    uint16 temp_L       : 1;        /**< Provide struct data type3's brief description here. */
  } bits;
} PalDs1629_TempRegType;

/**@cond -----------------------------------------------------------------------
 * Constants section (Project scope)
 * Add all constants with project scope here.
 * Capitalization: ALL_CAPS.
 * ----------------------------------------------------------------- @endcond */

/* TODO: Move this typedef to I2C Library */
#define I2C_READ_WRITE_MSK      0x01

#define PAL_DS1629_HALT_CLK         1
#define PAL_DS1629_RUN_CLK          0

#define PAL_DS1629_BREAKDOWN_ASCII_WDAY         0
#define PAL_DS1629_BREAKDOWN_ASCII_MON          4
#define PAL_DS1629_BREAKDOWN_ASCII_MDAY         8
#define PAL_DS1629_BREAKDOWN_ASCII_HOUR         11
#define PAL_DS1629_BREAKDOWN_ASCII_MIN          14
#define PAL_DS1629_BREAKDOWN_ASCII_SEC          17
#define PAL_DS1629_BREAKDOWN_ASCII_YEAR         20

#define PAL_DS1629_ACCESS_CONFIG_REG            0xAC
#define PAL_DS1629_START_CONV_REG               0xEE
#define PAL_DS1629_STOP_CONV_REG                0x22
#define PAL_DS1629_READ_TEMP_REG                0xAA
#define PAL_DS1629_READ_COUNTER_REG             0xA8
#define PAL_DS1629_READ_SLOPE_REG               0xA9
#define PAL_DS1629_ACCESS_CLK_REG               0xC0
#define PAL_DS1629_ACCESS_CLK_ALRM_REG          0xC7
#define PAL_DS1629_ACCESS_TH_REG                0xA1
#define PAL_DS1629_ACCESS_TL_REG                0xA2
#define PAL_DS1629_ACCESS_MEM_REG               0x17

/**@cond -----------------------------------------------------------------------
 * Macros section (Project scope)
 * Add all macros with project scope here.
 * Capitalization: ALL_CAPS.
 * ----------------------------------------------------------------- @endcond */

#define PAL_DS1629_BCD2INT(BCD_VALUE)       (uint8)( ((BCD_VALUE >> 4) * 10) + (BCD_VALUE % 16) )

#define PAL_DS1629_INT2BCD(INT_VALUE)       (int)( ((INT_VALUE/10) << 4) + (INT_VALUE % 10) )

#define PAL_DS1629_ASCII2BCD(ASCII_VALUE_H, ASCII_VALUE_L)  (uint8)( ( ( ( ASCII_VALUE_H - 0x30 ) << 4 ) & 0xF0 ) | ( ( ASCII_VALUE_L - 0x30 ) & 0x0F ) )

/**@cond -----------------------------------------------------------------------
 * Global variables section (Project scope)
 * Add global variables with project scope here. 
 * Capitalization: lower_case.
 * Declare them with 'extern' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Function prototypes section (Project scope)
 * Add prototypes for all functions with project scope here. 
 * Capitalization: lower_Camel_Case.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Getter/setter prototype section (Project scope)
 * Add prototypes for getter/setter methods here.
 * Capitalization: get_Camel_Case/is_Camel_Case/set_Camel_Case. 
 * ----------------------------------------------------------------- @endcond */

PalDs1629_AmPmType get_PalDs1629_AmPm(void);
void set_PalDs1629_AmPm(PalDs1629_AmPmType ampm);
uint8 PalDs1629_ByteRead( uint8_t command, PalDs1629_AddressType Ds1629Address );
Std_ReturnType PalDs1629_ByteWrite (uint8_t command, PalDs1629_AddressType Ds1629Address, uint8 DataByte);
void PalDs1629_Init (PalDs1629_ConfigType* PalDs1629_ConfigPtr);
Std_ReturnType PalDs1629_SetHourMode (PalDs1629_HourModeType PalDs1629_HourMode);
Std_ReturnType PalDs1629_SetTime (PalDs1629_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1629_SetDate (PalDs1629_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1629_GetTime (PalDs1629_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1629_GetDate (PalDs1629_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1629_GetTemp( PalDs1629_TempRegType* PalDs1629_TempValue );
char* PalDs1629_GetAscTemp( char* ascTempBuf );

#endif /* PAL_DS1629_H_ */


/** END OF FILE ********************* Copyright (C) 2012 Afflatus Techtronics */
